<?php
/**
 * @brief Kuaiqian 外快版支付插件类
 * @class Kuaiqian
 * @package App
 * @subpackage  Driver.Payment
 * @author cjli
 */
class Kuaiqian extends Payment implements Payment_Interface
{
	public function __construct()
	{
		$this->_paymentInfo = PaymentModel::get_payment_info('Kuaiqian');
	}
	
  public function getSettingFields()
  {
    return array(
      'kq_account' => '商户帐号',
      'kq_key' => '商户密钥',
    );
  }

  public function getSubmitForm($orderInfo)
  {
  	//字符集
        ///固定选择值：1、2、3
        ///1 代表UTF-8; 2代表GBK; 3 代表GB2312   默认值为1 根据页面编码选择
        $inputCharset = "1";

        //接受支付结果的页面地址
        ///需要是绝对地址，与bgUrl 不能同时为空 前台页面通知
        ///当bgUrl 为空时，快钱直接将支付结果get 到pageUrl 当bgUrl 不为空时，按照bgUrl的方式返回
        //$pageUrl = "http://127.0.0.1/waikaphp/receive.php";
			$pageUrl = "http://www.dudufr.com/99bill_back.php";

        //服务器接受支付结果的后台地址
        ///需要是绝对地址，与pageUrl 不能同时为空  后台服务器通知
        ///快钱将支付结果发送到bgUrl 对应的地址，并且获取商户按照约定格式输出的地址，显示页面给用户
        //支付结果通知地址
		$bgUrl = "http://www.dudufr.com/99bill_back_s.php";
		
		//网关地址
		$target="http://www.dudufr.com/99bill_from.php";	

        //网关版本
        ///固定值：v1.0
        $version="v1.0";

        //网关页面显示语言种类
        ///固定值：2  2 代表英文显示
    global $Language_id;
    $language_id = isset($orderInfo['language_id']) ? $orderInfo['language_id'] : $language_id;
  	$languageInfo = SiteModel::get_language_info($language_id);
		 if ($languageInfo['code'] = "fr") {
			 $Language = '3';
		 }
		 else {
			 $Language = '2';
		 }
        //$language = "3";

        ///渠道类型 1-VISA 2-MASTER 3-AMEX 7-JCB
        $channelType = "1";

        ///交易方式编号，channelType不为空时，该值可为空
        $mid = "";

        //签名类型
        ///固定值：1
        ///1 代表SHA-1 签名
        $signType = "1";

        //外卡账户
        ///接收款项的商户外卡账户编号 不同于快钱帐号
        //$merchantAcctId = "104110045110203";
		$merchantAcctId='812440056510030';

        //终端编号
        ///帐号开通后有个跟外卡帐号对应的终端编号
        //$termId = "00000203";//"00000259";
		$termId='01001955';

		//商户私钥证书
		//$pem_path='99911001122020490.pem';
		$pem_path='81244005651003090.pem';

        //持卡人 First Name
        ///中国名指姓氏，
        $firstName = $orderInfo['delivery_first_name'];

        //持卡人 Last Name
        ///中国名指名字
        $lastName = $orderInfo['delivery_last_name'];

        //持卡人 Full Name
        ///中国名指全名
        $fullName = $orderInfo['delivery_first_name']." ".$orderInfo['delivery_last_name'];

        //发卡银
        ///发卡银行银，支付卡所属银行
        $issuingBank='';

        //发卡国家
        ///发卡国家,卡所属国家
        $issuingCountry= $orderInfo['delivery_country'];
		//$issuingCountry= '';

        //持卡人电子邮件地址
        ///持卡人电子邮件地址
			$email = $orderInfo['delivery_email'];

        //持卡人联系电话
        ///持卡人联系电话
        $phoneNumber = $orderInfo['delivery_phone'];

        //billing address 对应的zip code
        ///账单地的邮政编码
        $zipCode = $orderInfo['delivery_postcode'];

        //shipping Address 对应的zip code
        ///商品的邮寄地址
        $shippingAddress =  '';

        //billing address
        ///账单邮寄详细地址
        //$billingAddress =   $order['address'].'&nbsp;&nbsp;'.$order['address1'].'&nbsp;&nbsp;'.$order['address2'];

			$billingAddress = $orderInfo['delivery_address'];
			$billingAddress .= $orderInfo['delivery_address2'] ? '&nbsp;&nbsp;'.$orderInfo['delivery_address2'] : null;
			$billingAddress .= $orderInfo['delivery_address3'] ? '&nbsp;&nbsp;'.$orderInfo['delivery_address3'] : null;

        //billing City
        ///账单城市
        $billingCity = $orderInfo['delivery_city'];

        //billing State
        ///账单洲
        $billingState = $orderInfo['delivery_province'];

        //billing Country
        ///账单国家
        $billingCountry = '';

        //商户订单号
        ///只允许使用字母、数字、- 、_,并以字母或数字开头
        ///每商户提交的订单号，必须在自身账户交易中唯一
		$orderId = $orderInfo['order_sn'];

        //订单报价币别
        ///货币符号，如USD、EUR 等
        ///货币符号  币别
        ///CNY  人民币
        ///USD  美元
        ///HKD  港元
        ///GBP  英镑
        ///JPY  日元
        ///EUR  欧元
        ///TWD  新台币
		//$pricingCurrency ='USD';
		$pricingCurrency = $_SESSION['currency'];
      
        //订单报价金额
        //金额乘以100。例如，美元298.30，提交时金额应为29830
		//$pricingAmount = $order['order_amount'] * 100;
		
  	$pricingAmount = currency($orderInfo['pay_amount']*100, null, false); //金额

        ///如果参数exchangeRateFlag为0，则为空，快钱按扣款币别为人民币处理。
        ///如果参数exchangeRateFlag为1，则由商户提供，不能为 空。
        $billingCurrency = "CNY";         //这个只有CNY，现在只有美元到人民币

        //订单扣款金额
        ///金额乘以100。例如，美元298.30，提交时金额应为29830
        ///如果参数exchangeRateFlag为0，则为空，快钱按照统一汇率换算成人民币。
        ///如果参数exchangeRateFlag为1，则由商户提供，不能为空。
        $billingAmount="";

        //汇率提供标志
        ///0 由快钱统一提供汇率
        ///1 由商户提交交易时提供
        $exchangeRateFlag="0";

        //汇率方向
        ///0 报价币别/扣款币别
        ///1 扣款币别/报价币别
        $exchangeRateDirection="";

        //汇率
        ///如果参数exchangeRateFlag为0，则为空
        ///如果参数exchangeRateFlag为1，则不能为空
        $exchangeRate="";

        //商户订单提交时间
        ///格式为：年[4 位]月[2 位]日[2 位]时[2 位]分[2 位]秒[2 位]
        ///例如：20071117020101
		$orderTime =  date('YmdHis', $order['start_time']);

        //商品名称
        ///英文字符串
		 $productName = '';

        //商品数量
        ///整型数字，只标识该订单的商品数量，默认填1
		$productNum= '1';

		//商品描述
		//英文字符串
		$productDesc = '';

        //商品代码
        ///字母、数字或 - 、_ 的组合,并以字母或数字开头
        ///商户自定义
			$productId='';

        //支付方式
        ///固定值 20
        ///20：外卡支付网关保留专用
        $payType="20";

        //银行代码
        ///用户在实际支付时所使用的银行代码
        ///扩展字段
        $bankId='';

        //固定选择值： 1、0
        ///默认为0
        ///1 代表同一订单号只允许提交1 次；
        ///0 表示同一订单号在没有支付成功的前提下可重复提交多次。
        ///建议实物购物车结算类商户采用 0；虚拟产品类商户采用1；
        $redoFlag="0";

        //合作伙伴在快钱的用户编号
        ///用户登录快钱首页后可查询到。仅适用于快钱合作伙伴中系统及平台提供商。
        ///扩展字段
        $pid = "";

        //收货方式
				$rcvMethord = "lowcost";

        //扩展字段一
        ///支付完成后，按照原样返回给商户
        $ext1="";

        //扩展字段二
        ///支付完成后，按照原样返回给商户
        $ext2="";

        $signMsgVal=""; //将非空的参数组成字符串
        $signMsgVal = $this->appendParam($signMsgVal, "inputCharset", $inputCharset);
        $signMsgVal = $this->appendParam($signMsgVal, "pageUrl", $pageUrl);
        $signMsgVal = $this->appendParam($signMsgVal, "bgUrl", $bgUrl);
        $signMsgVal = $this->appendParam($signMsgVal, "version", $version);
        $signMsgVal = $this->appendParam($signMsgVal, "language", $language);
        $signMsgVal = $this->appendParam($signMsgVal, "signType", $signType);
        $signMsgVal = $this->appendParam($signMsgVal, "merchantAcctId", $merchantAcctId);
        $signMsgVal = $this->appendParam($signMsgVal, "termId", $termId);
        $signMsgVal = $this->appendParam($signMsgVal, "email", $email);
        $signMsgVal = $this->appendParam($signMsgVal, "phoneNumber", $phoneNumber);
        $signMsgVal = $this->appendParam($signMsgVal, "zipCode", $zipCode);
        $signMsgVal = $this->appendParam($signMsgVal, "orderId", $orderId);
        $signMsgVal = $this->appendParam($signMsgVal, "pricingCurrency", $pricingCurrency);
        $signMsgVal = $this->appendParam($signMsgVal, "pricingAmount", $pricingAmount);
        $signMsgVal = $this->appendParam($signMsgVal, "billingCurrency", $billingCurrency);
        $signMsgVal = $this->appendParam($signMsgVal, "billingAmount", $billingAmount);
        $signMsgVal = $this->appendParam($signMsgVal, "exchangeRateFlag", $exchangeRateFlag);
        $signMsgVal = $this->appendParam($signMsgVal, "exchangeRateDirection", $exchangeRateDirection);
        $signMsgVal = $this->appendParam($signMsgVal, "exchangeRate", $exchangeRate);
        $signMsgVal = $this->appendParam($signMsgVal, "orderTime", $orderTime);
        $signMsgVal = $this->appendParam($signMsgVal, "productNum", $productNum);
        $signMsgVal = $this->appendParam($signMsgVal, "productId", $productId);
        $signMsgVal = $this->appendParam($signMsgVal, "payType", $payType);
        $signMsgVal = $this->appendParam($signMsgVal, "bankId", $bankId);
        $signMsgVal = $this->appendParam($signMsgVal, "redoFlag", $redoFlag);
        $signMsgVal = $this->appendParam($signMsgVal, "pid", $pid);
        $signMsgVal = $this->appendParam($signMsgVal, "ext1", $ext1);
        $signMsgVal = $this->appendParam($signMsgVal, "ext2", $ext2);
        //echo $signMsgVal;

			// fetch private key from file and ready it
			$fp = fopen($pem_path, "r");
			$priv_key = fread($fp, 8192);
			fclose($fp);
			$pkeyid = openssl_get_privatekey($priv_key);
			
			// compute signature
			openssl_sign($signMsgVal, $signMsg, $pkeyid,OPENSSL_ALGO_SHA1);
			
			// free the key from memory
			openssl_free_key($pkeyid);
			
			$signMsg = base64_encode($signMsg);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



        $def_url  = '<form id="frm" name="kqPay" style="text-align:center;" method="post" action="'.$target.'" target="_blank">';
			$def_url .= '<input type="hidden" name="inputCharset" value="'.$inputCharset.'"/>';
            $def_url .= '<input type="hidden" name="pageUrl" value="'.$pageUrl.'"/>';
			$def_url .= '<input type="hidden" name="bgUrl" value="'.$bgUrl.'"/>';
			$def_url .= '<input type="hidden" name="version" value="'.$version.'"/>';
			$def_url .= '<input type="hidden" name="language" value="'.$language.'"/>';
			$def_url .= '<input type="hidden" name="channelType" value="'.$channelType.'"/>';
			$def_url .= '<input type="hidden" name="mid" value="'.$mid.'"/>';
			$def_url .= '<input type="hidden" name="signType" value="'.$signType.'"/>';
			$def_url .= '<input type="hidden" name="signMsg" value="'.$signMsg.'"/>';
			$def_url .= '<input type="hidden" name="merchantAcctId" value="'.$merchantAcctId.'"/>';
            $def_url .= '<input type="hidden" name="termId" value="'.$termId.'"/>';
            $def_url .= '<input type="hidden" name="firstName" value="'.$firstName.'"/>';
            $def_url .= '<input type="hidden" name="lastName" value="'.$lastName.'"/>';
            $def_url .= '<input type="hidden" name="fullName" value="'.$fullName.'"/>';
            $def_url .= '<input type="hidden" name="issuingBank" value="'.$issuingBank.'"/>';
            $def_url .= '<input type="hidden" name="issuingCountry" value="'.$issuingCountry.'"/>';
            $def_url .= '<input type="hidden" name="email" value="'.$email.'"/>';
            $def_url .= '<input type="hidden" name="phoneNumber" value="'.$phoneNumber.'"/>';
            $def_url .= '<input type="hidden" name="zipCode" value="'.$zipCode.'"/>';
            $def_url .= '<input type="hidden" name="shippingAddress" value="'.$shippingAddress.'"/>';
            $def_url .= '<input type="hidden" name="billingAddress" value="'.$billingAddress.'"/>';
            $def_url .= '<input type="hidden" name="billingCity" value="'.$billingCity.'"/>';
            $def_url .= '<input type="hidden" name="billingState" value="'.$billingState.'"/>';
            $def_url .= '<input type="hidden" name="billingCountry" value="'.$billingCountry.'"/>';
            $def_url .= '<input type="hidden" name="orderId" value="'. $orderId.'"/>';
            $def_url .= '<input type="hidden" name="pricingCurrency" value="'.$pricingCurrency.'"/>';
            $def_url .= '<input type="hidden" name="pricingAmount" value="'.$pricingAmount.'"/>';
            $def_url .= '<input type="hidden" name="billingCurrency" value="'.$billingCurrency.'"/>';
            $def_url .= '<input type="hidden" name="billingAmount" value="'.$billingAmount.'"/>';
            $def_url .= '<input type="hidden" name="exchangeRateFlag" value="'.$exchangeRateFlag.'"/>';
            $def_url .= '<input type="hidden" name="exchangeRateDirection" value="'.$exchangeRateDirection.'"/>';
            $def_url .= '<input type="hidden" name="exchangeRate" value="'.$exchangeRate.'"/>';
            $def_url .= '<input type="hidden" name="orderTime" value="'.$orderTime.'"/>';
            $def_url .= '<input type="hidden" name="productName" value="'.$productName.'"/>';
            $def_url .= '<input type="hidden" name="productNum" value="'.$productNum.'"/>';
            $def_url .= '<input type="hidden" name="productId" value="'.$productId.'"/>';
            $def_url .= '<input type="hidden" name="productDesc" value="'.$productDesc.'"/>';
            $def_url .= '<input type="hidden" name="payType" value="'.$payType.'"/>';
            $def_url .= '<input type="hidden" name="bankId" value="'.$bankId.'"/>';
            $def_url .= '<input type="hidden" name="redoFlag" value="'.$redoFlag.'"/>';
            $def_url .= '<input type="hidden" name="ext1" value="'.$ext1.'"/>';
			$def_url .= '<input type="hidden" name="ext2" value="'.$ext2.'"/>';
            $def_url .= '<input type="hidden" name="pid" value="'.$pid.'"/>';
			$def_url .= '<input type="hidden" name="rcvMethord" value="'.$rcvMethord.'"/>';

			$def_url .= '<input type="hidden" name="sitename" value="'.$sitename.'"/>';
			$def_url .= "<input class='small_pay_button' type='submit' name='submit' value='" . L('_PAY_NOW_') . "' />";
			$def_url .= "</form></br>";

        return $def_url;
  }
  
 	 private function checkGet()
  	{
       $merchant_acctid     = '812440056510030';                 //人民币账号 不可空      

  		$merchantAcctId='';
			if(isset ($_REQUEST["merchantAcctId"])){
                $merchantAcctId = $_REQUEST["merchantAcctId"];
           }else{
               $merchantAcctId = '';

           } 

            //终端编号
            ///外卡网关终端编号
            $termId = '';
			if(isset ($_REQUEST["termId"])){
                $termId = $_REQUEST["termId"];
           }else{
               $termId = '';

           } 

            //网关版本
            ///固定值 v1.0
            ///注意为小写字母
            $version = '';
			if(isset ($_REQUEST["version"])){
                $version = $_REQUEST["version"];
           }else{
               $version = '';

           } 

            //网关页面显示语言种类
            ///固定值：2
            ///2 代表英文显示
            $language = '';
			if(isset ($_REQUEST["language"])){
                $language = $_REQUEST["language"];
           }else{
               $language = '';

           } 

            //签名类型
            ///固定值：1
            ///回  1 代表SHA-1 签名
            $signType = '';
			if(isset ($_REQUEST["signType"])){
                $signType = $_REQUEST["signType"];
           }else{
               $signType = '';

           } 

            //支付方式
            ///固定值：20
            ///20：外卡支付网关保留专
            $payType = '';
			if(isset ($_REQUEST["payType"])){
                $payType = $_REQUEST["payType"];
           }else{
               $payType = '';

           } 

            //银行代码
            ///返回用户在实际支付时所使用的银行代码
            ///扩展字段
			$bankId = '';
            if(isset ($_REQUEST["bankId"])){
                $bankId = $_REQUEST["bankId"];
           }else{
               $bankId = '';

           }                   

            //商户订单号
            ///只允许使用字母、数字、- 、_,并以字母或数字开头每商户提交的订单号，必须在自身账户交易中唯一
            $orderId = '';
			if(isset ($_REQUEST["orderId"])){
                $orderId = $_REQUEST["orderId"];
           }else{
               $orderId = '';

           } 

            //商户订单提交时间
            ///格式为：年[4 位]月[2 位]日[2 位]时[2 位]分[2 位]秒[2 位]例如：20071117020101
            $orderTime = '';
            if(isset ($_REQUEST["orderTime"])){
                $orderTime = $_REQUEST["orderTime"];
           }else{
               $orderTime = '';

           } 

            //订单报价
            ///货币符号，如USD、EUR 等
            ///货币符号请参考
            ///货币符号  币别
            ///CNY  人民币
            ///USD  美元
            ///HKD  港元
            ///GBP  英镑
            ///JPY  日元
            ///EUR  欧元
            ///TWD  新台币
            $pricingCurrency = '';
			if(isset ($_REQUEST["pricingCurrency"])){
                $pricingCurrency = $_REQUEST["pricingCurrency"];
           }else{
               $pricingCurrency = '';

           } 

            //订单报价金额
            ///金额乘以100。例如，美元298.30，提交时金额应为29830
             $pricingAmount= '';
			 if(isset ($_REQUEST["pricingAmount"])){
                $pricingAmount = $_REQUEST["pricingAmount"];
           }else{
               $pricingAmount = '';

           } 

            //订单扣款币别
            ///货币符号，如USD、EUR 等
            ///如果参数exchangeRateFlag 为0，快钱按照扣款币别为人民币处理，则返回为”CNY”。
            ///如果参数exchangeRateFlag 为1，则按商户提交信息原样返回。
              $billingCurrency= '';
			  if(isset ($_REQUEST["billingCurrency"])){
                $billingCurrency = $_REQUEST["billingCurrency"];
           }else{
               $billingCurrency = '';

           } 

            //
            ///金额乘以100。例如，美元298.30，提交时金额应为29830
            ///如果参数exchangeRateFlag 为0，快钱按照扣款币别为人民币处理，则返回换算后的人民币金额。
            ///如果参数exchangeRateFlag 为1，则按商户提交信息原样返回。
            $billingAmount = '';
			if(isset ($_REQUEST["billingAmount"])){
                $billingAmount = $_REQUEST["billingAmount"];
           }else{
               $billingAmount = '';

           } 

            //汇率提供标志
            ///0 由快钱统一提供汇率
            ///1 由商户提交交易时提供
            $exchangeRateFlag = '';
			if(isset ($_REQUEST["exchangeRateFlag"])){
                $exchangeRateFlag = $_REQUEST["exchangeRateFlag"];
           }else{
               $exchangeRateFlag = '';

           } 

            //汇率方向
            ///0 报价币别/扣款币别   1 扣款币别/报价币别
            ///如果参数exchangeRateFlag 为整型数字 0，则由快钱返回。
            ///如果exchangeRateFlag为1，则按商户提交信息原样返回。
            $exchangeRateDirection = '';
			if(isset ($_REQUEST["exchangeRateDirection"])){
                $exchangeRateDirection = $_REQUEST["exchangeRateDirection"];
           }else{
               $exchangeRateDirection = '';

           } 

            //汇率
            ///交易时汇率
            ///如果参数exchangeRateFlag 为整型数字 0，则由快钱返回。
            ///如果exchangeRateFlag为1，则按商户提交信息原样返回。
             $exchangeRate = '';
			 if(isset ($_REQUEST["exchangeRate"])){
                $exchangeRate = $_REQUEST["exchangeRate"];
           }else{
               $exchangeRate = '';

           } 

            //快钱交易号
            ///该交易在快钱系统中对应的交易号
             $dealId = '';
			 if(isset ($_REQUEST["dealId"])){
                $dealId = $_REQUEST["dealId"];
           }else{
               $dealId = '';

           } 

            //银行交易交易号
            ///该交易在银行支付时对应的交易号，如果不是通过银行卡支付，则为空
            /// 扩展字段
              $bankDealID= '';
			  if(isset ($_REQUEST["bankDealID"])){
                $bankDealID = $_REQUEST["bankDealID"];
           }else{
               $bankDealID = '';

           } 

            //快钱交易时间
            ///格式为：年[4 位]月[2 位]日[2 位]时[2 位]分[2 位]秒[2位]
            ///例如：20071117020101
             $dealTime= '';
            $dealTime = $_REQUEST["dealTime"];
			            if(isset ($_REQUEST["dealTime"])){
                $dealTime = $_REQUEST["dealTime"];
           }else{
               $dealTime = '';

           } 

            //实际支付金额
            ///返回在使用优惠券等情况后，用户实际支付的金额
            ///金额乘以100。例如，美元298.30，提交时金额应为29830
             $payAmount= '';
			 if(isset ($_REQUEST["payAmount"])){
                $payAmount = $_REQUEST["payAmount"];
           }else{
               $payAmount = '';

           } 

            //支付币别
            ///货币符号，如USD、EUR 等
            ///货币符号  币别
            ///CNY  人民币
            ///USD  美元
            ///HKD  港元
            ///GBP  英镑
            ///JPY  日元
            ///EUR  欧元
            ///TWD  新台币
              $payCurrency= '';
			  if(isset ($_REQUEST["payCurrency"])){
                $payCurrency = $_REQUEST["payCurrency"];
           }else{
               $payCurrency = '';

           } 

            //费用
            ///快钱收取商户的手续费，单位为分。
            $fee = '';
			if(isset ($_REQUEST["fee"])){
                $fee = $_REQUEST["fee"];
           }else{
               $fee = '';

           } 

            //处理结果
            ///10：支付成功   11：支付失败
             $payResult = '';
			            if(isset ($_REQUEST["payResult"])){
                $payResult = $_REQUEST["payResult"];
           }else{
               $payResult = '';

           } 

            //错误代码
            ///如果处理结果返回11，则有errCode 和errMessage
            ///错误代码和错误信息详细资料见参考资料。
            $errCode  = '';
			if(isset ($_REQUEST["errCode"])){
                $errCode = $_REQUEST["errCode"];
           }else{
               $errCode = '';

           } 

            //错误信息
            ///错误代码的英文描述。
            ///如果处理结果返回11，则有errCode和errMessage 错误代码和错误信息详细资料见图表 4 错误代码表
             $errMessage = '';
			 if(isset ($_REQUEST["errMessage"])){
                $errMessage = $_REQUEST["errMessage"];
           }else{
               $errMessage = '';

           } 

            //扩展字段一
            ///支付完成后，按照原样返回给商户
            $ext1  = '';
			if(isset ($_REQUEST["ext1"])){
                $ext1 = $_REQUEST["ext1"];
           }else{
               $ext1 = '';

           } 

            //扩展字段二
            ///支付完成后，按照原样返回给商户
            $ext2  = '';
			if(isset ($_REQUEST["ext2"])){
                $ext2 = $_REQUEST["ext2"];
           }else{
               $ext2 = '';

           } 

            //签名字符串
            ///快钱利用证书对发送数据进行签名
            $signMsg = '';
			if(isset ($_REQUEST["signMsg"])){
                $signMsg = $_REQUEST["signMsg"];
           }else{
               $signMsg = '';

           } 

        //将不为空参数值组成字符串
        //快钱返回给商户时的签名串示例如下（假定全部参数值都不为空）：
        $signMsgVal='';//将非空的参数组成字符串
        $signMsgVal = $this->appendParam($signMsgVal, "merchantAcctId", $merchantAcctId);
        $signMsgVal = $this->appendParam($signMsgVal, "termId", $termId);
        $signMsgVal = $this->appendParam($signMsgVal, "version", $version);
        $signMsgVal = $this->appendParam($signMsgVal, "language", $language);
        $signMsgVal = $this->appendParam($signMsgVal, "signType", $signType);
        $signMsgVal = $this->appendParam($signMsgVal, "payType", $payType);
        $signMsgVal = $this->appendParam($signMsgVal, "bankId", $bankId);
        $signMsgVal = $this->appendParam($signMsgVal, "orderId", $orderId);
        $signMsgVal = $this->appendParam($signMsgVal, "orderTime", $orderTime);
        $signMsgVal = $this->appendParam($signMsgVal, "pricingCurrency", $pricingCurrency);
        $signMsgVal = $this->appendParam($signMsgVal, "pricingAmount", $pricingAmount);
        $signMsgVal = $this->appendParam($signMsgVal, "billingCurrency", $billingCurrency);
        $signMsgVal = $this->appendParam($signMsgVal, "billingAmount", $billingAmount);
        $signMsgVal = $this->appendParam($signMsgVal, "exchangeRateFlag", $exchangeRateFlag);
        $signMsgVal = $this->appendParam($signMsgVal, "exchangeRateDirection", $exchangeRateDirection);
        $signMsgVal = $this->appendParam($signMsgVal, "exchangeRate", $exchangeRate);
        $signMsgVal = $this->appendParam($signMsgVal, "dealId", $dealId);
        $signMsgVal = $this->appendParam($signMsgVal, "bankDealID", $bankDealID);
        $signMsgVal = $this->appendParam($signMsgVal, "dealTime", $dealTime);
        $signMsgVal = $this->appendParam($signMsgVal, "payAmount", $payAmount);
        $signMsgVal = $this->appendParam($signMsgVal, "payCurrency", $payCurrency);
        $signMsgVal = $this->appendParam($signMsgVal, "fee", $fee);
        $signMsgVal = $this->appendParam($signMsgVal, "payResult", $payResult);
        $signMsgVal = $this->appendParam($signMsgVal, "errCode", $errCode);
        $signMsgVal = $this->appendParam($signMsgVal, "ext1", $ext1);
        $signMsgVal = $this->appendParam($signMsgVal, "ext2", $ext2);

//初始化结果及地址

        //开始验签
			$fp = fopen("vpos_cp4PHP.cer", "r"); //加载证书
			$cert = fread($fp, 8192);
			
			$signMsg = base64_decode($signMsg);
			fclose($fp);
			$pubkeyid = openssl_get_publickey($cert);
			// state whether signature is okay or not
			$ok = openssl_verify($signMsgVal, $signMsg, $pubkeyid); //验签
			//$ok = '1';


			if($merchant_acctid != $merchantAcctId){
				 return false;
			}else{
				if($ok == 1){
						if($payResult == '10'){
							return true;
						}elseif($payResult == '12'){
							return true;
						}else{
							return false;
						}								
				}else{
					return false;
				}
				
			}
  }

  public function callback()
  {
    if ($this->checkGet()) {
      $result = array(
        'verified' => true,
        'paidAmount' => $_REQUEST["pricingAmount"],
        'paymentId' => $_REQUEST['code'],
        'orderNumber' => $_REQUEST["orderId"],
        'updateOrderStatus' => false,
        'message' => '',
      );
    } else {
      $result = array(
        'verified' => false,
      );
    }
    if($_REQUEST["payResult"] == 10 || $_REQUEST["payResult"] == 12){
				$result['verified'] = true;
				$result['updateOrderStatus'] = true;
				$result['message'] = L('_PAYMENT_SUCCESS_');
			} else {
				$result['message'] = L('_PAYMENT_FAILURE_');
    }
    
    return $result;
  }

  
	private function appendParam($signMsgVal,$paramId,$paramValue)
	{
    if ($signMsgVal != "")
    {
        if($paramValue !=''){
            $signMsgVal .= "&" .$paramId . "=" .$paramValue;
        }
        
    }
    else
    {
        if($paramValue !=''){
           $signMsgVal = $paramId . "=" . $paramValue;
        }
        
    }
    return $signMsgVal;
	}
}
